package com.jiyinit.web.core.common.util;

import com.jiyinit.core.framework.base.util.DateUtil;
import com.jiyinit.core.framework.base.util.LogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;


public class MultipartFileUtil {

    /**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(MultipartFileUtil.class);

    /**
     * 单文件上传
     *@author lcq
     *@date 2018/1/7 10:21
     *@param
     *@return
     */
    public static File getFileByMultipartFile(MultipartFile mFile) {
        if(mFile == null){
            return null;
        }
        String orgFileName = mFile.getOriginalFilename();
        String dateTimeStr = DateUtil.dateFormat(new Date(), "yyyyMMddHHmmss");
        StringBuilder path = new StringBuilder(GlobalsUtils.getValue("EXCEL_UPLOAD_PATH"));
        path.append(dateTimeStr).append("-").append(orgFileName);

        String fileFullPath = path.toString();
        File file = new File(fileFullPath);
        if(!file.exists()){
            file.mkdirs();
        }
        try{
            mFile.transferTo(file);
        }catch (Exception e){
            LogUtil.error(LOGGER ,"e:{}", e);
        }
        return file;
    }

    public static boolean deleteServerFile(String filePath){
        boolean delete_flag = false;
        File file = new File(filePath);
        if (file.exists() && file.isFile() && file.delete()) delete_flag = true;
        else delete_flag = false;
        return delete_flag; }


}
